<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book List</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
    <div class="container-fluid">
        <!-- header-keyword -->
        <div class="row" id="header">
            <div class="col-md-10">
                <input type="text" class="form-control" id="keyword" placeholder="keyword">
            </div>
            <div class="col-md-2">
                <button type="button" class="btn btn-info" id="btn_search">Search</button>
            </div>
        </div>
        <!-- nav -->
        <div class="row">
            <div class="col-md-6"></div>
            <div class="col-md-6" align="right" >
                <button type="button" id="btn_login" class="btn btn-link btn-sm">Sign in</button>
                <button type="button" id="btn_register" class="btn btn-link btn-sm">Register</button>
                <button type="button" id="btn_logout" class="btn btn-link btn-sm">Logout</button>
                <button class="btn btn-info btn-sm" id="btn_cart" type="button">
                    Cart <span class="badge" id="cartnum">0</span>
                </button>
            </div>
        </div>

        <!-- main -->
        <div class="row" id="main">
            <div class="col-md-12">
                <!-- bookList -->
                <div id="bookList">
                    <div class="col-md-3 menu_left">
                        <h3>Category</h3>
                        
                        <div id="Catagory">
                            <div class="col-md-12">
                                <button type="button" class="btn btn-link btn-sm">History</button>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-9" style="border-left: 2px solid #000;">
                        <button type="button" class="btn btn-link btn-sm">Home</button>
                        <h3 id="searchres">All Books</h3>

                        <div class="col-md-12" align="right">
                            <button type="button" class="btn btn-info btn-xs" id="btn_sort">Sort by Price (Highest) </button>
                        </div>

                        <div class="col-md-12" id="books">
                            <div class="col-md-3">
                                <h3><a href="">The Creature Choir</a></h3>
                                <img src="images/book_1.jpeg" width="100%" alt="" srcset="">
                                <div class="detail" style="margin-top:1rem;font-weight: bold;">
                                    <p>NEW ARRIVAL!</p>
                                    <p>Author: Word Willms </p>
                                    <p>Publisher: HarperCollins Publishers</p>
                                    <p>Price: $117</p>
                                </div>
                            </div>

                            <div class="col-md-3">
                                <h3><a href="">Olive,Again</a></h3>
                                <img src="images/book_2.jpeg" width="100%" alt="" srcset="">
                                <div class="detail">
                                    <p>NEW ARRIVAL!</p>
                                    <p>Author: Word Willms </p>
                                    <p>Publisher: HarperCollins Publishers</p>
                                    <p>Price: $117</p>
                                </div>
                            </div>

                            <div class="col-md-3">
                                <h3><a href="">Have You Filled A Bucket Today? : A Guide to Daliy Happiness for kids</a></h3>
                                <img src="images/book_3.jpeg" width="100%" alt="" srcset="">
                                <div class="detail">
                                    <p>NEW ARRIVAL!</p>
                                    <p>Author: Word Willms </p>
                                    <p>Publisher: HarperCollins Publishers</p>
                                    <p>Price: $117</p>
                                </div>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function(){
            //API URL
            var apiurl="http://localhost:8080";

            //sort
            var sort="asc";
            var category="";
            var getnewid=false;
            var newbookid="";

            function islogin(){
                var currentuser=JSON.parse(sessionStorage.getItem("book_user"));
                console.log(currentuser);
                if(currentuser==undefined || currentuser==null){
                    // alert("Invalid login，please login again.");
                    // window.location.href="login.html";
                    $("#btn_login").show();
                    $("#btn_register").show();
                    $("#btn_logout").hide();
                }else{
                    $("#btn_login").hide();
                    $("#btn_register").hide();
                    $("#btn_logout").show();
                }
            }

            islogin();
            get_Category();
            get_Books();
            

            //Category
            function get_Category(){
                $.ajax({
                    url: apiurl+"/Category.php",
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        // console.log(data);
                        if(data.code==0){
                            var el_cate=$("#Catagory");
                            el_cate.html("");
                            var cate_list=data.data;
                            var html="";
                            for(var i=0;i<cate_list.length;i++){
                                var str='<div class="col-md-12">'+
                                '<button type="button" val="'+cate_list[i].category+'" class="btn btn-link btn-sm btn_cate">'+cate_list[i].category+'</button>'+
                                '</div>';
                                html+=str;
                            }
                            el_cate.html(html);
                        }else{
                            alert(data.msg);
                        }
                    }
                });
            }

            //Books
            function get_Books(){
                var book_name=$("#keyword").val();
                var params={book_query:book_name,book_category:category,book_order:sort};
                $.ajax({
                    url: apiurl+"/Books.php",
                    type: "POST",
                    dataType: "json",
                    data:params,
                    success: function (data) {
                        console.log(data);
                        if(data.code==0){
                            
                            var el_book=$("#books");
                            el_book.html("");
                            var book_list=data.data.books;
                            var html="";
                            for(var i=0;i<book_list.length;i++){
                                if(i==book_list.length-1 && !getnewid){
                                    getnewid=true;
                                    newbookid=book_list[i].id;
                                    console.log(book_list[i].name)
                                }

                                var newclass=newbookid==book_list[i].id?"isnew":"notnew";
                                var str='<div class="col-md-3" style="min-height:60rem;">'+
                                '<h3><a href="detail.html?id='+book_list[i].id+'">'+book_list[i].name+'</a></h3>'+
                                '<img src="'+book_list[i].img+'" width="100%" alt="" srcset="">'+
                                '<h4 class='+newclass+'>NEW ARRIVAL!</h4>'+
                                '<div class="detail" style="margin-top:1rem;font-weight: bold;">'+
                                '<p>Author: '+book_list[i].author+' </p>'+
                                '<p>Publisher: '+book_list[i].publishr+'</p>'+
                                '<p>Price: $'+book_list[i].price+'</p>'+
                                '</div>'+
                                '</div>';
                                html+=str;
                            }
                            el_book.html(html);
                        }else{
                            alert(data.msg);
                        }
                    }
                });

                var book_cart=sessionStorage.getItem("book_cart");
                if(book_cart==null || book_cart==undefined){
                    $("#cartnum").text(0);
                }else{
                    book_cart=JSON.parse(book_cart);
                    $("#cartnum").text(book_cart.length);
                }
            }

            //Search
            $("#btn_search").click(function(){
                get_Books();
                $("#searchres").text("Searching Results");
            });

            //Sort
            $("#btn_sort").click(function(){
                sort=sort=="asc"?"desc":"asc";
                get_Books();
                $("#searchres").text("All Books (Sort By Highest Price)");
            });

            //Login
            $("#btn_login").click(function(){
                window.location.href="login.html";
            });
            //Register
            $("#btn_register").click(function(){
                window.location.href="createaccount.html";
            });


            //Category click
            $('#Catagory').on('click','button',function(){
                var val=$(this).attr("val");
                category=val;
                $("#searchres").text("All "+val);
                get_Books();
            })
            

            //Logout
            $("#btn_logout").click(function(){
                $.ajax({
                    url: apiurl+"/Logout.php",
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        sessionStorage.removeItem("book_user");
                        $($("body")[0]).html('<h2 style="text-align:center;color:red;">Logging out</h2>');
                        setInterval(() => {
                            window.location.href="index.html";
                        }, 3000);
                    }
                });
            });

            $("#btn_cart").click(function(){
                window.location.href="mycart.html";
            });

        })
    </script>
    <style>
        .isnew{
    color: red;
    display: block;
}
.notnew{
    display: none;
}
    </style>
</body>
</html>